package com.ykt.service;

import com.jfinal.aop.Enhancer;
import com.ykt.common.thirdapi.polyv.PolyvHelper;
import com.ykt.common.thirdapi.polyv.model.PolyvVideo;
import com.ykt.common.utils.StringUtil;
import com.ykt.model.Video;

import java.util.List;

/**
 * @author long.hua
 * @version 1.0.0
 * @since 2016-05-26 23:17
 */
public class PolyvService {

    private Video videoDao = Video.dao;

    private PolyvHelper polyvHelper = Enhancer.enhance(PolyvService.class.getName(), PolyvHelper.class);

    /**
     * 循环视频列表，保存到数据库
     */
    public void syncVideo() {
        int numPage = 1;
        int numPerPage = 1000;

        while (true) {
            List<PolyvVideo> list = polyvHelper.getNewList(numPage, numPerPage);
            if (list == null || list.isEmpty()) {
                break;
            }

            this.saveVideoList(list);

            numPage++;
        }
    }

    private void saveVideoList(List<PolyvVideo> videos) {
        for (PolyvVideo polyvVideo : videos) {
            Video entity = videoDao.findByVid(polyvVideo.getVid());

            boolean notExist = (entity == null);
            if (notExist) {
                entity = new Video();
            }

            entity.set(Video.swf_link, polyvVideo.getSwf_link());
            entity.set(Video.status, polyvVideo.getStatus());
            entity.set(Video.tag, polyvVideo.getTag());
            entity.set(Video.seed, polyvVideo.getSeed());
            entity.set(Video.playerwidth, polyvVideo.getPlayerwidth());
            entity.set(Video.title, polyvVideo.getTitle());
            entity.set(Video.duration, polyvVideo.getDuration());
            entity.set(Video.df, polyvVideo.getDf());
            entity.set(Video.first_image, polyvVideo.getFirst_image());
            entity.set(Video.times, polyvVideo.getTimes());
            entity.set(Video.context, polyvVideo.getContext());
            entity.set(Video.original_definition, polyvVideo.getOriginal_definition());
            entity.set(Video.playerheight, polyvVideo.getPlayerheight());
            entity.set(Video.ptime, polyvVideo.getPtime());
            entity.set(Video.cataid, polyvVideo.getCataid());
            entity.set(Video.mp4, concatMp4(polyvVideo));
            entity.set(Video.flv, polyvVideo.getCataid());
            entity.set(Video.hls, concatHls(polyvVideo));
            entity.set(Video.vid, polyvVideo.getVid());

            if (notExist) {
                entity.save();
            } else {
                entity.update();
            }
        }
    }

    private static String concatMp4(PolyvVideo polyvVideo) {
        String mp4 = "";
        if (polyvVideo.getMp4_1() != null) {
            mp4.concat(polyvVideo.getMp4_1()).concat(",");
        }
        if (polyvVideo.getMp4_2() != null) {
            mp4.concat(polyvVideo.getMp4_2()).concat(",");
        }
        if (polyvVideo.getMp4_3() != null) {
            mp4.concat(polyvVideo.getMp4_3()).concat(",");
        }
        return mp4;
    }

    private String concatHls(PolyvVideo polyvVideo) {
        String mp4 = polyvVideo.getHls();
        if (mp4 != null) {
            mp4 = StringUtil.trimRight(mp4, "]");
            mp4 = StringUtil.trimLeft(mp4, "[");
        }
        return mp4;
    }

    public static void main(String[] args) {
        new PolyvService().syncVideo();
    }

}
